set quiet

export RUSTFLAGS := "-D warnings"
export RUSTDOCFLAGS := "-D warnings"

target := shell("rustc -vV | sed -n 's|host: ||p'")

# List all available tasks
help:
  just --list --justfile {{ justfile() }}

# Install the project (use `prefix` to specify the installation prefix)
[group('installation')]
[confirm('Continue installation? [y/N]')]
install prefix='/usr/local': (build "--release")
  #!/usr/bin/env sh
  if ! test -d {{ prefix }}; then
    echo "The installation prefix '{{ prefix }}' does not exist." >&2
    exit 1
  else
    test -w {{ prefix }} && INSTALL="install" || INSTALL="sudo install"
  fi
  $INSTALL -Dm755 target/{{ target }}/release/cs2s-ctl {{ prefix }}/bin/cs2s-ctl
  $INSTALL -Dm755 target/{{ target }}/release/libcs2s_ccapi.so {{ prefix }}/lib/libcs2s_ccapi.so
  $INSTALL -Dm644 crates/cs2s-ccapi/cs2s-ccapi.h {{ prefix }}/include/cs2s-ccapi.h

# Uninstall the project (use `prefix` to specify the installation prefix)
[group('installation')]
[confirm('Continue uninstallation? [y/N]')]
uninstall prefix='/usr/local':
  #!/usr/bin/env sh
  if ! test -d {{ prefix }}; then
    echo "The installation prefix '{{ prefix }}' does not exist." >&2
    exit 1
  else
    test -w {{ prefix }} && RM="rm" || RM="sudo rm"
  fi
  $RM -f {{ prefix }}/bin/cs2s-ctl
  $RM -f {{ prefix }}/lib/libcs2s_ccapi.so
  $RM -f {{ prefix }}/include/cs2s-ccapi.h

# Build the project
[group('development')]
build *args:
  cargo build --target {{ target }} {{ args }}

# Clean the project
[group('development')]
clean *args:
  cargo clean {{ args }}

# Lint the project
[group('development')]
lint *args: (fmt "--check") doc
  cargo check --workspace --all-targets --all-features {{ args }}
  cargo clippy --workspace --all-targets --all-features {{ args }}

# Test the project (use cargo-nextest if available)
[group('development')]
test *args:
  #!/usr/bin/env sh
  if $(which cargo-nextest >/dev/null 2>&1); then
    cargo nextest run --target {{ target }} \
      --workspace --all-features --all-targets --config-file .nextest.toml {{ args }}
  else
    cargo test --target {{ target }} \
      --workspace --all-features --all-targets {{ args }}
  fi

# Benchmark the project
[group('development')]
bench *args:
  cargo bench --target {{ target }} --workspace --all-features --all-targets {{ args }}

# Analyze the code coverage of the project (use cargo-nextest if available)
[group('development')]
lcov *args:
  #!/usr/bin/env sh
  if ! $(which cargo-llvm-cov >/dev/null 2>&1); then
    echo 'cargo-llvm-cov is not installed.'
    exit 1
  fi
  if $(which cargo-nextest >/dev/null 2>&1); then
    cargo +nightly llvm-cov --target {{ target }} nextest --workspace --all-features {{ args }}
  else
    cargo +nightly llvm-cov --target {{ target }} --workspace --all-features {{ args }}
  fi

# Format the code of the project
[group('development')]
fmt *args:
  cargo fmt --all {{ args }}

# Generate the documentation of the project
[group('development')]
doc *args:
  cargo doc --workspace --all-features --no-deps {{ args }}
